/*
 * @Description: 
 * @Author: cw
 * @LastEditors: cw
 * @Date: 2019-03-29 09:27:19
 * @LastEditTime: 2019-05-06 17:00:27
 */
let moment = require('moment');
let request = require('request');
let crypto = require('crypto');
let DataHelper=require("./DataHelper");
const STR_FORMAT = require('string-format');

function cryptMd5(sData) {
    var md5 = crypto.createHash('md5');
    return md5.update(sData).digest('hex');
}

(function(window) {
    function ProcessAdmobData(sBody){
        if(sBody==""){
            return null;
        }
        let tRow = sBody.split('\n');
        let tMap = [];
        let tHeads = [
            "Date","AppId","GameName","Platform","AdUnitName","AdUnit","Country","AdRequests","Clicks","Views","Revenue","Avaliable"
        ];
        for(let i=0;i<tRow.length;++i){
            let tRowData = tRow[i].split(',');
            let tData = {};
            let tConvertedData = {}
            for(let j=0;j<tRowData.length;++j){
                let sVal = tRowData[j].replace(/^\"|\"$/g,'');
                if(tHeads[j]=="Date"){
                    tData[tHeads[j]]= sVal.split(' ')[0];
                }else if(tHeads[j]=="AppId"){
                    tData[tHeads[j]] = sVal.substring(2)
                }else{
                    tData[tHeads[j]]=parseFloat(sVal) || sVal;
                }
            }
            tConvertedData["APP_ID"] = tData["AppId"];
            tConvertedData["APP_NAME"] = tData["GameName"];
            tConvertedData["AD_UNIT"] = tData["AdUnit"];
            tConvertedData["COUNTRY"] = tData["Country"];
            tConvertedData["PLATFORM"] = tData["Platform"];
            tConvertedData["REVENUE"] = tData["Revenue"];
            tConvertedData["REQUEST"] = tData["AdRequests"];
            tConvertedData["CLICKED"] = tData["Clicks"];
            tConvertedData["VIEWS"] = tData["Views"];
            tConvertedData["COMPLETES"] = tData["Views"];
            tConvertedData["AVALABLE"] = tData["Avaliable"];
            tConvertedData["DATE"] = tData["Date"];
            tMap.push(tConvertedData);
        }
        let tDayData = {}
        for(let tRowData of tMap){
            let sAppID = tRowData.APP_ID
            if(sAppID){
                let sCountry = tRowData.COUNTRY
                let sAdUnit = tRowData.AD_UNIT
                if(!tDayData[sAppID]){
                    tDayData[sAppID] = {}
                }
                if(!tDayData[sAppID]){
                    tDayData[sAppID] = {}
                }
                if(!tDayData[sAppID][sCountry]){
                    tDayData[sAppID][sCountry] = {}
                }
                if(!tDayData[sAppID][sCountry][sAdUnit]){
                    tDayData[sAppID][sCountry][sAdUnit]={REQUEST:0,AVALABLE:0,VIEWS:0,REVENUE:0,COMPLETES:0,CLICKED:0}
                }
                let tAdDayData = tDayData[sAppID][sCountry][sAdUnit];
                tAdDayData.REQUEST+=parseInt(tRowData.REQUEST);
                tAdDayData.AVALABLE+=parseInt(tRowData.AVALABLE);
                tAdDayData.VIEWS+=parseInt(tRowData.VIEWS);
                tAdDayData.CLICKED+=parseInt(tRowData.CLICKED);
                tAdDayData.COMPLETES+=parseInt(tRowData.COMPLETES);
                tAdDayData.REVENUE+=parseFloat(tRowData.REVENUE);    
            }
        }
        return tDayData
    }

    function ProcessFacebookData(tBody){
        if(!tBody||!tBody.data||!tBody.data[0]||!tBody.data[0].results){
            return {};
        }
        let tDataMap = {android:{},ios:{},unknown:{}}
        let tResults = tBody.data[0].results;
        for(let tResult of tResults){
            let sPlatform = tResult.breakdowns[0].value;
            let sCountry = tResult.breakdowns[1].value;
            let nPlacementId = tResult.breakdowns[2].value;
            if(tDataMap[sPlatform][nPlacementId]==null){
                tDataMap[sPlatform][nPlacementId] = {};
            }
            if(!tDataMap[sPlatform][nPlacementId][sCountry]){
                tDataMap[sPlatform][nPlacementId][sCountry]={};
            }
            tDataMap[sPlatform][nPlacementId][sCountry][tResult.metric] = tResult.value;
        }
        for(let sPlacementId in tDataMap.unknown){
            let tPlacement = tDataMap.unknown[sPlacementId];
            let tPlatformPlacement = tDataMap.android[sPlacementId];
            if(!tPlatformPlacement){
                tPlatformPlacement = tDataMap.ios[sPlacementId];
            }
            if(tPlatformPlacement){
                for(let country in tPlacement){
                    if(!tPlatformPlacement[country]){
                        tPlatformPlacement[country] = tPlacement[country];
                    }else{
                        let tPlatformCountryData = tPlatformPlacement[country];
                        for(let metric in tPlacement[country]){
                            if(!tPlatformCountryData[metric]){
                                tPlatformCountryData[metric]=parseFloat(tPlacement[country][metric]);
                            }else{
                                tPlatformCountryData[metric]=parseFloat(tPlatformCountryData[metric])+parseFloat(tPlacement[country][metric]);
                            }
                        }
                    }
                }
            }
        }

        let tMap = [];
        tDataMap.unknown=undefined;
        for(let sPlatform in tDataMap){
            let tPlatformData = tDataMap[sPlatform];
            for(let sPlacementId in tPlatformData){
                let tPlacementData = tPlatformData[sPlacementId];
                for( let sCountry in tPlacementData){
                    let tConvertedData = {}
                    let tMetricData = tPlacementData[sCountry];
                    tConvertedData["AD_UNIT"] = sPlacementId;
                    tConvertedData["COUNTRY"] = sCountry.toUpperCase();
                    tConvertedData["PLATFORM"] = sPlatform;
                    tConvertedData["REVENUE"] =  tMetricData["fb_ad_network_revenue"] || 0;
                    tConvertedData["REQUEST"] = tMetricData["fb_ad_network_request"] || 0;
                    tConvertedData["VIEWS"] = tMetricData["fb_ad_network_imp"] || 0;
                    tConvertedData["AVALABLE"] = tMetricData["fb_ad_network_filled_request"] || 0;
                    tConvertedData["CLICKED"] = tMetricData["fb_ad_network_click"] || 0;
                    tConvertedData["COMPLETES"] = 0;
                    tConvertedData["DATE"] = "";
                    tMap.push(tConvertedData);
                }
            }
        }
        let tDayData = {}
        for(let tRowData of tMap){
            let sCountry = tRowData.COUNTRY
            let sAdUnit = tRowData.AD_UNIT
            let sPlatForm = tRowData.PLATFORM;
            if(!tDayData[sPlatForm]){
                tDayData[sPlatForm] = {};
            }
            if(!tDayData[sPlatForm][sCountry]){
                tDayData[sPlatForm][sCountry] = {}
            }
            if(!tDayData[sPlatForm][sCountry][sAdUnit]){
                tDayData[sPlatForm][sCountry][sAdUnit]={REQUEST:0,AVALABLE:0,VIEWS:0,REVENUE:0,COMPLETES:0,CLICKED:0}
            }
            let tAdDayData = tDayData[sPlatForm][sCountry][sAdUnit];
            tAdDayData.REQUEST+=parseInt(tRowData.REQUEST);
            tAdDayData.AVALABLE+=parseInt(tRowData.AVALABLE);
            tAdDayData.VIEWS+=parseInt(tRowData.VIEWS);
            tAdDayData.CLICKED+=parseInt(tRowData.CLICKED);
            tAdDayData.COMPLETES+=parseInt(tRowData.COMPLETES);
            tAdDayData.REVENUE+=parseFloat(tRowData.REVENUE);    
        }
        return tDayData
    }

    function RequestAdmobData(sDate,tGpToken,pCallback){
        DataHelper.Instance.DoQueryAdmob(tGpToken,sDate,(sErr,tRet)=>{
            if(!sErr){
                let tAdmobData = ProcessAdmobData(tRet);
                pCallback(null,tAdmobData);
            }
        });
    }
    
    function RequestFacebookData(sDate,sToken,sID,pCallback){
        DataHelper.Instance.DoQueryFacebook(sDate,sToken,sID,(sErr,tRet)=>{
            if(!sErr){
                let tFacebookData=ProcessFacebookData(tRet);
                pCallback(null,tFacebookData)
            }else{
                pCallback(sErr)
            }
        })
    }

if (typeof exports !== "undefined") {
    exports.RequestAdmobData=RequestAdmobData;
    exports.RequestFacebookData=RequestFacebookData;
}
else {
    window.RequestAdmobData=RequestAdmobData;
    window.RequestFacebookData=RequestFacebookData;

    if (typeof define === "function" && define.amd) {
        define(function() {
            return {
                RequestAdmobData:RequestAdmobData,
                RequestFacebookData:RequestFacebookData,
            }
        })
    }
}
})(typeof window === "undefined" ? this : window);